package com.jdz.gxq.info.admin.estate.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jdz.gxq.info.admin.estate.bo.Estate;
import com.jdz.gxq.info.admin.estate.form.EditEstateForm;
import com.jdz.gxq.info.admin.estate.service.EstateService;
import com.jdz.gxq.info.admin.file.bo.ImageInfo;
import com.jdz.gxq.info.admin.file.service.FileService;
import com.jdz.gxq.info.common.action.CommonAction;

public class EditEstateAction extends CommonAction {

	private EstateService estateService = null;
	
	private FileService fileService = null;
	 
	public ActionForward doExecute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		EditEstateForm editEstateForm = (EditEstateForm) form;
	
		Estate estate = estateService.getEstateById(editEstateForm.getId());
		
		List<ImageInfo> imageList = new ArrayList<ImageInfo>();
		String picIds = estate.getEnterprisePicture();
		if (!StringUtils.isEmpty(picIds)) {
			String[] picIdArray = picIds.split(",");
			
			for (String picId : picIdArray) {
				ImageInfo imageInfo = fileService.getImageInfo(picId);
				
				if (imageInfo != null) {
					imageList.add(imageInfo);
				}
			}
		}
		
		editEstateForm.setEstate(estate);
		editEstateForm.setImageList(imageList);
		editEstateForm.setOldEnterprisePic(picIds);
		
		return mapping.findForward("success");
	}

	public void setEstateService(EstateService estateService) {
		this.estateService = estateService;
	}

	public void setFileService(FileService fileService) {
		this.fileService = fileService;
	}
}

